package com.cdream.basic.algorithm.leetcode.linkedlist;

import java.util.LinkedList;

/**
 * @author cdream
 * @date 6/4/21
 */
public class Question160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        LinkedList<ListNode> l1 = new LinkedList<>();
        LinkedList<ListNode> l2 = new LinkedList<>();
        ListNode temp = headA;
        while (temp != null) {
            l1.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while (temp != null) {
            l2.add(temp);
            temp = temp.next;
        }
        ListNode res = null;
        while (l1.size() != 0 && l2.size() != 0) {
            ListNode pop1 = l1.removeLast();
            ListNode pop2 = l2.removeLast();
            if (pop1 == pop2) {
                res = pop1;
            }
            if (pop1 != pop2) {
                break;
            }
        }
        return res;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}
